#!/usr/bin/python

import sympy

class Matrix(sympy.Matrix):
    def __init__(self, values):
        matrix = values
        l = sympy.Symbol('l')
        for i in range(len(matrix)):
            matrix[i][i] = 1 - l
            super(Matrix, self).__init__(matrix)

    def _get_lambda(self):
        det = self.det('berkowitz')
        ls = sympy.solve(det, 'l')
        abs = [sympy.Abs(x) for x in ls]
        return max(abs)


def main():
    """
    Test function.
    """
    #	matrix = Matrix([1, 5, 6, 7, 1.0/5, 1, 4, 6, 1.0/6, 1.0/4, 1, 4, 1.0/7, 1.0/6, 1.0/4, 1])
    #	print matrix
    #	matrix.solve()
    l = sympy.Symbol('l')
    matrix = Matrix([
        [1 - l, 5, 6, 7],
        [1.0 / 5, 1 - l, 4, 6],
        [1.0 / 6, 1.0 / 4, 1 - l, 4],
        [1.0 / 7, 1.0 / 6, 1.0 / 4, 1 - l]
    ])
    print matrix._get_lambda()


if __name__ == "__main__":
    main()

